package com.scqkzqtz.basesdk.utils;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.scqkzqtz.basesdk.R;


/**
 * 分享弹框
 * Created by hef on 2017/12/13.
 */
public class ShareWindow extends PopupWindow implements View.OnClickListener {

    private Activity activity;
    private Window window;
    private WindowManager.LayoutParams lp;
    private LayoutInflater inflater;
    private LinearLayout weChatLinear;
    private LinearLayout weChatMomentsLinear;
    private LinearLayout qqLinear;
    private LinearLayout qzoneLinear;
    private TextView cancelBtn;

    public interface OnShareWindowShowListener {
        void show(int position);
    }

    private OnShareWindowShowListener onShareWindowShowListener;

    public void setOnShareWindowShowListener(OnShareWindowShowListener onShareWindowShowListener) {
        this.onShareWindowShowListener = onShareWindowShowListener;
    }

    public ShareWindow(Activity activity) {
        super(activity);
        this.activity = activity;
        inflater = LayoutInflater.from(activity);
        initView();
    }

    private void initView() {
        window = activity.getWindow();
        lp = window.getAttributes();

        View view = inflater.inflate(R.layout.dialog_share, null);
        this.setContentView(view);

        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.setAnimationStyle(R.style.AnimBottom);
        ColorDrawable dw = new ColorDrawable();
        this.setBackgroundDrawable(dw);

        DisplayMetrics metrics = activity.getResources().getDisplayMetrics();
        int screenWidth = metrics.widthPixels;

        weChatLinear = view.findViewById(R.id.share_wechat);
        weChatMomentsLinear = view.findViewById(R.id.share_wechat_moments);
        qqLinear = view.findViewById(R.id.share_qq);
        qzoneLinear = view.findViewById(R.id.share_qqzone);
        cancelBtn = view.findViewById(R.id.share_cancel_btn);

        weChatLinear.setMinimumWidth(screenWidth / 4);
        weChatMomentsLinear.setMinimumWidth(screenWidth / 4);
        qqLinear.setMinimumWidth(screenWidth / 4);
        qzoneLinear.setMinimumWidth(screenWidth / 4);

        weChatLinear.setOnClickListener(this);
        weChatMomentsLinear.setOnClickListener(this);
        qqLinear.setOnClickListener(this);
        qzoneLinear.setOnClickListener(this);
        cancelBtn.setOnClickListener(this);

        //弹框消失
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                lp.alpha = 1f;
                window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                window.setAttributes(lp);
            }
        });
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.share_wechat) {
            if (onShareWindowShowListener != null)
                onShareWindowShowListener.show(0);
        }
        if (id == R.id.share_wechat_moments) {
            if (onShareWindowShowListener != null)
                onShareWindowShowListener.show(1);
        }
        if (id == R.id.share_qq) {
            if (onShareWindowShowListener != null)
                onShareWindowShowListener.show(2);
        }
        if (id == R.id.share_qqzone) {
            if (onShareWindowShowListener != null)
                onShareWindowShowListener.show(3);
        }
        if (id == R.id.share_cancel_btn) {

        }
        dismiss();
    }

    /**
     * 显示弹框，出现阴影
     */
    public void show(View view) {//在视频界面分享，设置透明度会影响视频播放，使画面消失
        if (window == null || lp == null) return;
        lp.alpha = 0.8f;
        window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        window.setAttributes(lp);

        showAtLocation(view, Gravity.BOTTOM, 0, 0);
    }


}
